<template>
    <div
        class="dialogContent"
        v-loading="confirmLoading"
    >
        <el-form
            ref="configFormRef"
            :model="configForm"
            label-width="82px"
            class="demo-ruleForm"
            :inline="true"
        >
            <div
                v-for="(item, index) in configForm.relatedIndicatorList"
                :key="index"
            >
                <el-scrollbar>
                    <div class="dialogModel">
                        <el-row>
                            <el-col :span="7">
                                <el-form-item
                                    label="仪器"
                                    prop="title"
                                    style="width: 100%;"
                                >
                                    <el-select
                                        v-model="item.instrument"
                                        class="m-2"
                                        placeholder="请选择仪器"
                                    >
                                        <el-option
                                            v-for="m in item.instruments"
                                            :key="m.valueName"
                                            :label="m.valueName"
                                            :value="m.valueName"
                                        />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item
                                    label="方法"
                                    prop="title"
                                    style="width: 100%;"
                                >
                                    <el-select
                                        v-model="item.methodName"
                                        class="m-2"
                                        placeholder="请选择方法"
                                    >
                                        <el-option
                                            v-for="m in item.methodNames"
                                            :key="m.valueName"
                                            :label="m.valueName"
                                            :value="m.valueName"
                                        />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item
                                    label="环境"
                                    prop="title"
                                    style="width: 100%;"
                                >
                                    <el-input
                                        v-model="item.temperature"
                                        placeholder="温度°C"
                                        style="width: 76px;margin-right: 4px;"
                                    />
                                    <span style="margin-right: 16px;">°C</span>
                                    <el-input
                                        v-model="item.humidity"
                                        placeholder="湿度%"
                                        style="width: 76px;margin-right: 4px;"
                                    />
                                    <span>%</span>
                                </el-form-item>
                            </el-col>

                        </el-row>
                    </div>
                    <div class="dialogModel">
                        <div class="modelTitle">
                            <div>计算公式</div>
                        </div>
                        <el-tabs
                            v-model="item.formulaMainListActiveName"
                            class="demo-tabs"
                        >
                            <el-tab-pane
                                v-for="(m, n) in item.formulaMainList"
                                :key="n"
                                :label="m.formulaName"
                                :name="n"
                            >
                                <template #label>
                                    <span class="custom-tabs-label">
                                        <span>{{ m.formulaName }}</span>
                                    </span>
                                </template>
                                <el-row v-if="m.description">
                                    <el-col :span="24">
                                        <p style="padding-left: 20px">说明：{{ m.description }}</p>
                                    </el-col>
                                </el-row>
                                <el-scrollbar style="width: 96%;">
                                    <div class="inputValuesBox">
                                        <div class="inputValuesBoxItem">
                                            <div class="inputValuesTitle">
                                                <div
                                                    class="inputValuesItem"
                                                    style="text-align: center;"
                                                    :style="{ width: 740 + m.medianPos.length * 80 + 'px' }"
                                                >
                                                    今日检测
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 774px;text-align: center;display: flex;flex-direction: row;align-items: center;"
                                                >
                                                    <span style="margin: 0 auto;">今日恒重(备用)</span>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 630px;text-align: center;"
                                                >
                                                    1日前恒重(备用)
                                                </div>

                                            </div>
                                            <div class="inputValuesTitle">
                                                <!-- <div
                                                        class="inputValuesItem"
                                                        style="width: 40px"
                                                    >
                                                    </div> -->
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px;padding-right: 10px;"
                                                >
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px"
                                                >
                                                    <div
                                                        class="el-col-content"
                                                        style="text-align: center;"
                                                    >
                                                        样品名称/编号
                                                    </div>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    :style="{ width: 476 + m.medianPos.length * 80 + 'px' }"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'V' || j.valueName == 'm3' || j.valueName == 'm4' || j.valueName == 'm3_1' || j.valueName == 'm3_2' || j.valueName == 'm3_3' || j.valueName == 'sampleName'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;text-align: center;padding: 1px 11px;"
                                                            >
                                                                {{ j.valueName }}
                                                                <el-tooltip
                                                                    class="box-item"
                                                                    effect="dark"
                                                                    :content="`${j.meaning}(${j.unit || '--'})`"
                                                                    placement="top"
                                                                >
                                                                    <el-icon
                                                                        color="#ccc"
                                                                        style="margin-left: 2px;"
                                                                    >
                                                                        <QuestionFilled />
                                                                    </el-icon>
                                                                </el-tooltip>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px; border-left: 1px dashed #58be3f;padding-left: 15px;"
                                                >
                                                    <div
                                                        class="el-col-content"
                                                        style="text-align: center;"
                                                    >
                                                        样品名称/编号
                                                    </div>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 630px"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'm1_0'|| j.valueName == 'm1_1_0' || j.valueName == 'm1_2_0' || j.valueName == 'm1_3_0'||j.valueName == 'm2_0'|| j.valueName == 'm2_1_0' || j.valueName == 'm2_2_0' || j.valueName == 'm2_3_0'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;text-align: center;padding: 1px 11px;"
                                                            >
                                                                {{ j.valueName.replace('_0', '') }}
                                                                <el-tooltip
                                                                    class="box-item"
                                                                    effect="dark"
                                                                    :content="`${j.meaning}(${j.unit || '--'})`"
                                                                    placement="top"
                                                                >
                                                                    <el-icon
                                                                        color="#ccc"
                                                                        style="margin-left: 2px;"
                                                                    >
                                                                        <QuestionFilled />
                                                                    </el-icon>
                                                                </el-tooltip>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 1px;height: 42px; border-left: 1px dashed #58be3f;padding: 0 0 0 2px;"
                                                >
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    :style="{ width: 630  + 'px' }"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'm1'|| j.valueName == 'm1_1' || j.valueName == 'm1_2' || j.valueName == 'm1_3'||j.valueName == 'm2'|| j.valueName == 'm2_1' || j.valueName == 'm2_2' || j.valueName == 'm2_3'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;text-align: center;padding: 1px 11px;"
                                                            >
                                                                {{ j.valueName }}
                                                                <el-tooltip
                                                                    class="box-item"
                                                                    effect="dark"
                                                                    :content="`${j.meaning}(${j.unit || '--'})`"
                                                                    placement="top"
                                                                >
                                                                    <el-icon
                                                                        color="#ccc"
                                                                        style="margin-left: 2px;"
                                                                    >
                                                                        <QuestionFilled />
                                                                    </el-icon>
                                                                </el-tooltip>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- -------分割线-------- -->
                                            <div
                                                class="inputValuesTitle"
                                                v-for="(p, q) in m.inputValuesList"
                                                :key="q"
                                                v-show="relatedIndicatorActiveCode == p.currentActiveName"
                                            >
                                                <!-- <div
                                                        class="inputValuesItem"
                                                        style="width: 40px"
                                                    >
                                                        <el-checkbox
                                                            v-model="p.checked"
                                                            :true-value="1"
                                                            :false-value="0"
                                                            @change="(v) => relatedIndicatorCheckChange(v, n, q)"
                                                        />
                                                    </div> -->
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px;padding-right: 10px;"
                                                >
                                                    <p
                                                        v-if="!p.temporary"
                                                        style="width: 100%; text-align: right;"
                                                    >{{ p.dailyIndicator }}</p>

                                                    <el-input
                                                        v-else
                                                        v-model="p.dailyIndicator"
                                                        placeholder="样品来源"
                                                        style=""
                                                    />
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px;"
                                                >
                                                    <el-input
                                                        v-model="p['sampleName']"
                                                        disabled
                                                        v-show="!p.inputValuesListItemType || (p.inputValuesListItemType == 1 && !p.isAdd && p.temporaryType == 'day_1')"
                                                    />
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    :style="{ width: 476 + m.medianPos.length * 80 + 'px' }"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'V' || j.valueName == 'm3' || j.valueName == 'm4' || j.valueName == 'm3_1' || j.valueName == 'm3_2' || j.valueName == 'm3_3' || j.valueName == 'sampleName'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;padding: 0 5px;"
                                                            >
                                                                <el-input
                                                                    v-model="p[j.valueName]"
                                                                    :disabled="j.valueName == 'm3' "
                                                                    v-show="!p.inputValuesListItemType || (p.inputValuesListItemType == 1 && !p.isAdd && p.temporaryType == 'day_2')"
                                                                />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 120px;height: 42px; border-left: 1px dashed #58be3f;padding-left: 15px;"
                                                >
                                                    <el-input
                                                        v-model="p.sampleName_0"
                                                        :disabled="p.checked == 0"
                                                        v-show="!p.inputValuesListItemType || (p.inputValuesListItemType == 1 && p.isAdd) || (p.inputValuesListItemType == 1 && !p.isAdd && p.temporaryType == 'day_today')"
                                                    />
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 630px;"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'm1_0'|| j.valueName == 'm1_1_0' || j.valueName == 'm1_2_0' || j.valueName == 'm1_3_0'||j.valueName == 'm2_0'|| j.valueName == 'm2_1_0' || j.valueName == 'm2_2_0' || j.valueName == 'm2_3_0'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;padding: 0 5px;"
                                                            >
                                                                <el-input
                                                                    v-model="p[j.valueName]"
                                                                    :disabled="j.valueName == 'm1_0' || j.valueName == 'm2_0'"
                                                                    v-show="!p.inputValuesListItemType || (p.inputValuesListItemType == 1 && p.isAdd && p.temporaryType == 'day_today')"
                                                                />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width: 1px;height: 42px; border-left: 1px dashed #58be3f;padding: 0 0 0 2px;"
                                                >
                                                </div>
                                                <div
                                                    class="inputValuesItem"
                                                    style="width:630px"
                                                >
                                                    <div class="processValuePosStyle">
                                                        <div
                                                            class="processValuePosStyleItem"
                                                            v-for="(j,k) in m.processValuePos"
                                                            :key="k"
                                                            v-show="j.valueName == 'm1'|| j.valueName == 'm1_1' || j.valueName == 'm1_2' || j.valueName == 'm1_3'||j.valueName == 'm2'|| j.valueName == 'm2_1' || j.valueName == 'm2_2' || j.valueName == 'm2_3'"
                                                        >
                                                            <div
                                                                class="el-col-content"
                                                                style="width: 78px;display: inline-block;padding: 0 5px;"
                                                            >
                                                                <el-input
                                                                    v-model="p[j.valueName]"
                                                                    disabled
                                                                    v-show="!p.inputValuesListItemType || (p.inputValuesListItemType == 1 && !p.isAdd && p.temporaryType == 'day_1')"
                                                                />
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div
                                                    class="inputValuesItem"
                                                    v-if="p.temporary"
                                                >
                                                    <div
                                                        class="el-col-content el-col-content-mr"
                                                        @click="deleteTemporaryInspection(index, n, q)"
                                                        style="margin: 6px 0 0 6px"
                                                    >
                                                        <img
                                                            src="../../../../../assets/images/minus.png"
                                                            alt=""
                                                            style=" width: 20px;height: 20px;margin-right: 4px;"
                                                        >
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </el-scrollbar>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item
                                            label=" "
                                            prop="description"
                                            style="width: 100%;"
                                        >
                                            <div
                                                class="el-col-content el-col-content-mr"
                                                @click="addTemporaryInspection(index, n)"
                                                style="display: flex;flex-direction: row;align-items: center;"
                                            >
                                                <img
                                                    src="../../../../../assets/images/plus.png"
                                                    alt=""
                                                    style=" width: 20px;height: 20px;margin-right: 4px;"
                                                >
                                                <span>临时检测</span>
                                            </div>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item
                                            label="计算公式:"
                                            prop="expression"
                                            style="width: 100%;"
                                        >
                                            <div style="color: #58be3f;">{{ m.expression }}</div>
                                            <!-- <el-checkbox v-model="m.checked1"></el-checkbox> -->
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div class="dialogModel">
                        <div
                            class="modelTitle"
                            style="margin-bottom: 20px;"
                        >
                            <div>检测值</div>
                            <div class="operateIcon">
                                <el-button
                                    type="primary"
                                    size="small"
                                    @click="formulaResultvalue(item, index)"
                                >
                                    计算
                                </el-button>
                            </div>
                        </div>
                        <el-row v-if="item.decimalPlace">
                            <el-col :span="24">
                                <p style="padding: 10px 0 20px 24px">小数位：{{ item.decimalPlace }}</p>
                            </el-col>
                        </el-row>
                        <div v-if="item.formulaMainList && item.formulaMainList.length > 0">
                            <div
                                v-for="(p, q) in item.inputValuesList"
                                :key="q"
                                v-show="p.type == 'normal' || (p.inputValuesListItemType == 1 && p.temporaryType != 'day_toady')"
                            >
                                <el-form-item
                                    :label="p.dailyIndicator"
                                    prop="expression"
                                >
                                    <el-input
                                        style="width: 225px;"
                                        v-model="p.testResult"
                                    ></el-input>
                                </el-form-item>
                                <el-form-item
                                    label=""
                                    prop="expression"
                                    v-if="item.indicatorDetailList[q] && item.indicatorDetailList[q].standardScope && !_.isNil(item.indicatorDetailList[q].standardScope.min) && !_.isNil(item.indicatorDetailList[q].standardScope.max)"
                                >
                                    {{ rangeTransfor(item.indicatorDetailList[q].standardScope)}}
                                </el-form-item>
                            </div>
                        </div>
                    </div>
                </el-scrollbar>
            </div>
        </el-form>
    </div>
    <el-dialog
        v-model="showMPNImgVisible"
        title="对应表"
        width="1200"
    >
        <img
            src="@/assets/15.png"
            style=" width: 1168px;"
        >
    </el-dialog>
</template>

<script setup>
// utils
import Storage from "@/utils/storage.js";
import {
    QuestionFilled
} from "@element-plus/icons-vue";
import _ from 'lodash';
import moment from 'moment';
import { defineExpose, defineProps, nextTick, onMounted, ref, watch } from "vue";
// api
import taskHttp from "@/https/task.js";
import { ElMessage } from "element-plus";
const emits = defineEmits(["closed"]);
const props = defineProps({
    taskSearchForm: Object
})

const userId = ref();
userId.value = Storage.localGet("userId")

const configForm = ref({

})
const configFormRef = ref({})
const relatedIndicatorActiveName = ref(0)
const relatedIndicatorActiveCode = ref('MLVSS')
const relatedIndicatorchange = () => {
    relatedIndicatorActiveCode.value = configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].indicatorName
}

const formulaMainListActiveName = ref(0)
const operatorList = ref(['+', '-', '*', '/', '(', ')', '%'])
// 
const handleClickType = ref()
const handleClickTaskType = ref()
const handleClick = async (type, row) => {
    console.log(row, 'row')
    handleClickType.value = type
    handleClickTaskType.value = row.id
    if (type == 'report' || type == 'update' || type == 'edit' || type == 'detail' || type == 'review' || type == 'check') {
        const configRes = await taskHttp.getTaskPropertiesById(row.parentParentId || row.id)
        if (configRes.data.code == 200) {
            console.log(configRes.data.data, row.parentParentId || row.id)
            configRes.data.data.relatedIndicatorList.forEach((item, index) => {
                if (item.indicatorName == 'MLSS') {
                    configRes.data.data.relatedIndicatorList.splice(index, 1)
                }
            })
            console.log(configRes.data.data, 'configRes.data.data')
            let configItem = configRes.data.data
            configItem.taskId = row.parentParentId || row.id
            let arr = []
            let relationDailyItem = []
            console.log(configItem, 'configItem111111')
            configItem.relatedIndicatorList.forEach(m => {
                relationDailyItem.push(m.indicatorName)
                m.indicatorDetailList.forEach(p => {
                    p.result = ''
                    arr.push({
                        indicatorName: m.indicatorName,
                        ...p
                    })
                })
            })
            configItem.relationDailyItemName = relationDailyItem.join('、')
            configItem.relatedIndicatorPlatList = arr
            configForm.value = configItem
            console.log(configForm.value, 'configForm.value88888')
            const res = await taskHttp.queryTaskDetailById({ id: row.id })
            res.data.data.formulaMainList.splice(0, 2)
            let taskData = res.data.data
            renameChildren(taskData)
            taskData.formulaMainList.forEach(item => {
                if (item.calculationFormulaContent) {
                    item.calculationFormulaContent = JSON.parse(item.calculationFormulaContent)
                } else {
                    item.calculationFormulaContent = []
                }
                item.lineId = ''
                item.lineExpression = ''
                item.slope = ''
                item.rsquared = ''
                item.intercept = ''
                item.lineName = ''
                item.lineCategory = ''
                item.checked1 = true
                item.checked2 = row.id == 3280 || row.id == 3283 || row.id == 3286
                item.inputValuesList = []
                item.medianPos.forEach((k, l) => {
                    item.processValuePos.push({
                        backupDays: null,
                        formulaId: item.id,
                        id: null,
                        isBackup: null,
                        meaning: k.medianMeaning,
                        sort: item.processValuePos.length + l,
                        unit: null,
                        valueName: k.medianName,
                        medianFlag: true
                    })
                })
                // 此处为定制化需求
                let customizedList = [
                    { meaning: 'sampleName_0', valueName: 'sampleName_0' },
                    { meaning: '恒重后瓷坩埚质量', valueName: 'm1_0' },
                    { meaning: '瓷坩埚，第一次称量', valueName: 'm1_1_0' },
                    { meaning: '瓷坩埚，第二次称量', valueName: 'm1_2_0' },
                    { meaning: '瓷坩埚，第三次称量', valueName: 'm1_3_0' },
                    { meaning: '恒重后滤膜加瓷坩埚质量', valueName: 'm2_0' },
                    { meaning: '滤膜加瓷坩埚，第一次称量', valueName: 'm2_1_0' },
                    { meaning: '滤膜加瓷坩埚，第二次称量', valueName: 'm2_2_0' },
                    { meaning: '滤膜加瓷坩埚，第三次称量', valueName: 'm2_3_0' },
                ]

                customizedList.forEach((k, l) => {
                    item.processValuePos.push({
                        backupDays: null,
                        formulaId: item.id,
                        id: null,
                        isBackup: null,
                        meaning: k.meaning,
                        sort: item.processValuePos.length + l,
                        unit: null,
                        valueName: k.valueName,
                        medianFlag: true
                    })
                })
                configForm.value.relatedIndicatorList.forEach(m => {
                    m.temperature = ''
                    m.humidity = ''
                    m.instrument = ''
                    m.instruments = JSON.parse(taskData.instruments)
                    m.methodName = ''
                    m.methodNames = JSON.parse(taskData.methodNames)
                    m.decimalPlace = taskData.decimalPlace
                    m.sampleTime = ''
                    m.sampleTime_1 = ''
                    m.sampleTime_2 = ''
                    m.inputValuesList = []
                    m.indicatorDetailList.forEach(p => {
                        let inputValuesListItem = {
                            currentActiveName: m.indicatorName,
                            indicatorName: m.indicatorName,
                            dailyIndicator: p.dailyIndicator,
                            indicatorType: p.dailyIndicator,
                            relatedIndicator: p.relatedIndicator,
                            standardScope: p.standardScope,
                            formulaId: item.id,
                            inputValues: [],
                            inputValuesKey: [],
                            temporary: false,
                            result: '',
                            testResult: '',
                            hasParallelResult: '',
                            sampleName: '',
                            sampleName_0: '',
                            sampleName_2: '',
                            type: 'normal',
                            checked: 1,
                        }
                        item.processValuePos.forEach(k => {
                            inputValuesListItem[k.valueName] = ''
                            inputValuesListItem.inputValues.push(k.meaning)
                            inputValuesListItem.inputValuesKey.push(k.valueName)
                        })
                        item.inputValuesList.push(inputValuesListItem)
                        m.inputValuesList.push(inputValuesListItem)
                    })
                })
                item.processValuePos.forEach(m => {
                    m.value = ''
                    m.temporary = false
                })
                item.temporaryCalculationFormulaContent = []
            })

            console.log(configForm.value, 'configForm.value22222')
            configForm.value.relatedIndicatorList.forEach(item => {
                item.formulaMainList = JSON.parse(JSON.stringify(taskData.formulaMainList))
                item.formulaMainListActiveName = 0
            })
            // configForm.value.formulaMainList = taskData.formulaMainList
            relatedIndicatorActiveCode.value = configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].indicatorName
            queryIsReportHasResult(configForm.value.taskId)

        } else {
            ElMessage({
                message: configRes.data.msg,
                type: 'error'
            })
            return
        }
    } else if (type == 'editList') {

        const res = await taskHttp.labTaskReport({
            taskId: row.parentParentId || row.id,
            depId: props.taskSearchForm.depId,
            entryDate: moment(props.taskSearchForm.taskDate).format('YYYY-MM-DD'),
        })
        console.log(res, 'labTaskReport')
    }
}
// 查看是否有无填报
const queryIsReportHasResult = async (taskId) => {
    console.log(props.taskSearchForm.taskDate, 'props.taskDate----------')
    const res = await taskHttp.queryLastResult({
        taskId: taskId,
        depId: props.taskSearchForm.depId,
        entryDate: moment(props.taskSearchForm.taskDate).format('YYYY-MM-DD'),
    })
    console.log(res.data.data)

    if (res.data.code == 200 && res.data.data) {
        res.data.data.forEach(item => {
            if (item.indicatorType && item.indicatorType.indexOf('平行样') > -1) {
                item.type = 'parallel'
                item.dailyIndicator = item.indicatorType
            } else if (item.indicatorType && item.indicatorType.indexOf('质控') > -1) {
                item.type = 'qualityControl'
                item.dailyIndicator = item.indicatorType
            } else if (item.inputValuesListItemType == 1) {
                item.type = ''
            } else {
                item.type = 'normal'
                item.dailyIndicator = item.indicatorType
            }
        })
        configForm.value.relatedIndicatorList.forEach((item, index) => {
            item.temperature = res.data.data[0].temperature
            item.humidity = res.data.data[0].humidity
            item.instrument = res.data.data[0].instrument
            item.methodName = res.data.data[0].methodName
            item.sampleTime = res.data.data[0].sampleTime
            if (item.indicatorName == 'MLVSS') {
                // item.inputValuesList = []
                res.data.data.forEach((p, q) => {
                    if (p.indicatorName == 'MLVSS') {
                        p.inputValues = JSON.parse(p.inputValues)
                        p.inputValuesJson = JSON.parse(p.inputValuesJson)
                        p.temporaryType = 'day_today'
                        item.formulaMainList.forEach((k, l) => {
                            if (p.formulaId == k.id) {
                                console.log(p, '///////////////11111')
                                k.inputValuesList.forEach((m, n) => {
                                    if (p.indicatorType == m.indicatorType) {
                                        let newVal = { ...m, ...p, ...p.inputValuesJson }
                                        newVal.checked = 1
                                        if (n == 0) {
                                            console.log(p, m, newVal, '-----------')
                                        }
                                        item.formulaMainList[l].inputValuesList[n] = newVal
                                        item.inputValuesList[n] = newVal
                                        // console.log(p, m, newVal, '-----------')
                                    }
                                })
                            }
                        })

                        if (p.inputValuesListItemType == 1) {
                            p.currentActiveName = relatedIndicatorActiveCode.value
                            item.formulaMainList.forEach((k, l) => {
                                if (p.formulaId == k.id) {
                                    p.inputValuesKey = []
                                    for (let key in p.inputValuesJson) {
                                        p.inputValuesKey.push(key)
                                    }
                                    let newVal = { ...p, ...p.inputValuesJson }
                                    item.formulaMainList[l].inputValuesList.push(newVal)
                                    nextTick(() => {
                                        configForm.value.relatedIndicatorList[index].inputValuesList.push(newVal)
                                    })
                                }
                            })
                        }
                    }
                })
            }
        })
        console.log(configForm.value, '999999999')
    }
    // return
    const res_1 = await taskHttp.queryLastResult({
        taskId: taskId,
        depId: props.taskSearchForm.depId,
        entryDate: moment(props.taskSearchForm.taskDate).add(-1, 'day').format('YYYY-MM-DD'),
    })
    console.log(res_1.data.data)

    if (res_1.data.code == 200 && res_1.data.data) {
        configForm.value.relatedIndicatorList.forEach((item, index) => {
            console.log(item, 'res_1')
            if (item.indicatorName == 'MLVSS') {
                let inputValuesListIds = []
                item.inputValuesList.forEach(t => {
                    if (t.oldId) {
                        inputValuesListIds.push(t.oldId)
                    }
                })
                res_1.data.data.forEach((p, q) => {
                    if (p.indicatorName == 'MLVSS') {
                        p.inputValues = JSON.parse(p.inputValues)
                        p.inputValuesJson = JSON.parse(p.inputValuesJson)
                        p.temporaryType = 'day_1'
                        if (!p.oldId) { p.oldId = p.id }
                        item.formulaMainList.forEach((k, l) => {
                            if (p.formulaId == k.id) {
                                k.inputValuesList.forEach((m, n) => {
                                    if (p.indicatorType == m.indicatorType) {
                                        // m.sampleName_0 = p.sampleName
                                        for (let key in p.inputValuesJson) {
                                            if (key == 'm1' || key == 'm1_1' || key == 'm1_2' || key == 'm1_3' || key == 'm2' || key == 'm2_1' || key == 'm2_2' || key == 'm2_3') {
                                                m[key] = p.inputValuesJson[`${key}_0`]
                                            }
                                        }
                                        if (n == 0) {
                                            console.log(m, '-----------111')
                                        }
                                    }
                                })
                            }
                        })
                        if (p.inputValuesListItemType == 1) {
                            p.currentActiveName = relatedIndicatorActiveCode.value
                            p.dailyIndicator = p.indicatorType
                            item.formulaMainList.forEach((k, l) => {
                                if (p.formulaId == k.id) {
                                    if (inputValuesListIds.includes(p.id) || (p.oldId && inputValuesListIds.includes(p.oldId))) {
                                        console.log(p, 'ppppp')
                                        k.inputValuesList.forEach((m, n) => {
                                            if (p.indicatorType == m.indicatorType) {
                                                m.temporaryType = 'day_1'
                                                if (n == 0) {
                                                    console.log(p, m, '-----------')
                                                }

                                                // for (let key in p.inputValuesJson) {
                                                //     if (p.inputValuesListItemType != 1) {
                                                //         console.log(key, p.inputValuesJson[key], '-----------111key')
                                                //         if (key == 'P10' || key == 'P1' || key == 'P0.1') {
                                                //             item.formulaMainList[l].inputValuesList[n][key] = p.inputValuesJson[`${key}_1`]
                                                //         }
                                                //     }
                                                // }
                                            }
                                        })
                                    } else {
                                        p.inputValuesKey = []
                                        for (let key in p.inputValuesJson) {
                                            p.inputValuesKey.push(key)
                                        }
                                        let newVal = { ...p, ...p.inputValuesJson }
                                        // newVal.sampleName_0 = newVal.sampleName
                                        console.log(newVal, 'newVal')
                                        for (let key in p.inputValuesJson) {
                                            if (key.indexOf('_0') > -1) {
                                                if (key == 'm1' || key == 'm1_1' || key == 'm1_2' || key == 'm1_3' || key == 'm2' || key == 'm2_1' || key == 'm2_2' || key == 'm2_3') {
                                                    newVal[key.replace('_0', '')] = p.inputValuesJson[key]
                                                }
                                            }
                                        }

                                        item.formulaMainList[l].inputValuesList.push(newVal)

                                        nextTick(() => {
                                            configForm.value.relatedIndicatorList[index].inputValuesList.push(newVal)
                                        })
                                    }
                                }
                            })
                        }
                    }
                })
            }
        })
        console.log(configForm.value, '999999999')
    }

}
watch(
    () => configForm.value,
    (val) => {
        val.relatedIndicatorList.forEach(item => {
            console.log(item.formulaMainList, 'item')
            item.formulaMainList && item.formulaMainList.forEach(p => {
                p.inputValuesList.forEach(k => {
                    if (k['m3_3']) {
                        k['m3'] = k['m3_3']
                    } else if (k['m3_2']) {
                        k['m3'] = k['m3_2']
                    } else if (k['m3_1']) {
                        k['m3'] = k['m3_1']
                    } else {
                        k['m3'] = ''
                    }
                    if (k['m1_3_0']) {
                        k['m1_0'] = k['m1_3_0']
                    } else if (k['m1_2_0']) {
                        k['m1_0'] = k['m1_2_0']
                    } else if (k['m1_1_0']) {
                        k['m1_0'] = k['m1_1_0']
                    } else {
                        k['m1_0'] = ''
                    }
                    if (k['m2_3_0']) {
                        k['m2_0'] = k['m2_3_0']
                    } else if (k['m2_2_0']) {
                        k['m2_0'] = k['m2_2_0']
                    } else if (k['m2_1_0']) {
                        k['m2_0'] = k['m2_1_0']
                    } else {
                        k['m2_0'] = ''
                    }
                    if (k['m1_3']) {
                        k['m1'] = k['m1_3']
                    } else if (k['m1_2']) {
                        k['m1'] = k['m1_2']
                    } else if (k['m1_1']) {
                        k['m1'] = k['m1_1']
                    } else {
                        k['m1'] = ''
                    }
                    if (k['m2_3']) {
                        k['m2'] = k['m2_3']
                    } else if (k['m2_2']) {
                        k['m2'] = k['m2_2']
                    } else if (k['m2_1']) {
                        k['m2'] = k['m2_1']
                    } else {
                        k['m2'] = ''
                    }
                })
            })
        })
    },
    { deep: true }
)
const inputValuesboxList = ref([
    { code: 1, name: 1 },
    { code: 2, name: 2 },
    { code: 3, name: 3 }
])
const relatedIndicatorCheckChange = (v, n, q) => {
    console.log(v, n, q, 'v')
    configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].formulaMainList.forEach((item, index) => {
        if (v == 1) {
            if (index == n) {
                item.inputValuesList[q].checked = 1
                configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].inputValuesList[q] = configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].formulaMainList[n].inputValuesList[q]
            } else {
                item.inputValuesList[q].checked = 0
            }
        }
    })
}
// 递归字段名转换函数

function renameChildren (tree) {
    if (tree.hasOwnProperty('childrens')) {
        tree.childrenTasks = tree.childrens;
        delete tree.childrens;
    }
    if (tree.childrenTasks && Array.isArray(tree.childrenTasks)) {
        for (const child of tree.childrenTasks) {
            renameChildren(child);
        }
    }
}
// 增加临时检测
const addTemporaryInspection = (index, n) => {
    let org = configForm.value.relatedIndicatorList[index].formulaMainList[n]
    let inputValuesListItem = {
        currentActiveName: relatedIndicatorActiveCode.value,
        indicatorName: relatedIndicatorActiveCode.value,
        dailyIndicator: '临时检测',
        indicatorType: '',
        relatedIndicator: '',
        formulaId: configForm.value.relatedIndicatorList[index].formulaMainList[n].id,
        inputValues: [],
        inputValuesKey: [],
        temporary: true,
        result: '',
        type: '',
        unit: configForm.value.relatedIndicatorList[index].unit,
        testResult: '',
        hasParallelResult: '',
        sampleName: '',
        checked: 1,
        averageValue: '',
        hasParallelResultChecked: 0,
        averageValueChecked: 0,
        inputValuesListItemType: 1,
        temporaryType: 'day_today',
        isAdd: true
    }
    org.processValuePos.forEach(k => {
        inputValuesListItem[k.valueName] = ''
        inputValuesListItem.inputValues.push(k.meaning)
        inputValuesListItem.inputValues.push(k.meaning)
        inputValuesListItem.inputValuesKey.push(k.valueName)
    })
    if (org.lineId) {
        configForm.value.relatedIndicatorList[index].formulaMainList[n].inputValuesList.forEach(item => {
            inputValuesListItem.a = configForm.value.relatedIndicatorList[index].formulaMainList[n].slope
            inputValuesListItem.b = configForm.value.relatedIndicatorList[index].formulaMainList[n].intercept
        })
    }
    configForm.value.relatedIndicatorList[index].formulaMainList.forEach(item => {
        item.inputValuesList.push(JSON.parse(JSON.stringify(inputValuesListItem)))
    })
    configForm.value.relatedIndicatorList[index].inputValuesList.push(inputValuesListItem)
    console.log(configForm.value, '222222')
}
// 删除临时检测
const deleteTemporaryInspection = (index, n, q) => {
    console.log(index, n, q)
    configForm.value.relatedIndicatorList[index].formulaMainList[n].inputValuesList.splice(q, 1)
    configForm.value.relatedIndicatorList[index].inputValuesList.splice(q, 1)
}
// 通过公式id获取公式的index序列
const getFormulaMainIndexById = (id, relatedIndicatorListIndex) => {
    let formulaMainIndex = 0
    configForm.value.relatedIndicatorList[relatedIndicatorListIndex].formulaMainList.forEach((item, index) => {
        if (id == item.id) {
            formulaMainIndex = index
        }
    })
    return formulaMainIndex
}
// 计算
const formulaResultvalue = async (item, index) => {
    if (item.formulaMainList[item.formulaMainListActiveName].checked1) {
        const trialBalQueue = item.formulaMainList[item.formulaMainListActiveName].inputValuesList.map(async (item, index) => {
            if (item.currentActiveName == relatedIndicatorActiveCode.value) {

                let inputValues = {}
                item.inputValuesKey.forEach(key => {
                    inputValues[key] = item[key]
                })
                let params = {
                    formulaId: item.formulaId,
                    taskid: configForm.value.taskId,
                    inputValues: inputValues
                }

                return await taskHttp.trialBal(params)
            } else {
                return null
            }
        })
        const results = await Promise.all(trialBalQueue)
        console.log(results, 'results')

        let resultList = configForm.value.relatedIndicatorList[index]
        results.forEach((p, q) => {
            if (p.data && p.data.code == 200) {
                let pIdForWhichFormulaIndex = getFormulaMainIndexById(configForm.value.relatedIndicatorList[index].inputValuesList[q].formulaId, 0)
                console.log(pIdForWhichFormulaIndex)
                configForm.value.relatedIndicatorList[index].formulaMainList[pIdForWhichFormulaIndex].inputValuesList[q].result = p.data.data.data.result
                p.data.data.data.medianRusultVos.forEach(k => {
                    configForm.value.relatedIndicatorList[index].formulaMainList[item.formulaMainListActiveName].inputValuesList[q][k.medianName] = k.result
                })
                let currentResultItem = resultList.inputValuesList[q]
                currentResultItem.result = p.data.data.data.result

            } else {
                console.log(p)
                let pIdForWhichFormulaIndex = getFormulaMainIndexById(configForm.value.relatedIndicatorList[index].inputValuesList[q].formulaId, 0)
                console.log(pIdForWhichFormulaIndex)
                let t = configForm.value.relatedIndicatorList[index].formulaMainList[pIdForWhichFormulaIndex].inputValuesList[q]
                if (t.checked == 1 && t.type != 'parallelBlank') {
                    ElMessage({
                        type: 'error',
                        message: configForm.value.relatedIndicatorList[index].formulaMainList[pIdForWhichFormulaIndex].inputValuesList[q].dailyIndicator + ' 计算错误' + '\n' + p.data.msg
                    })
                }
            }
        })
        resultList.inputValuesList.forEach((k, j) => {
            k.testResult = k.result
        })
    } else {
        ElMessage({
            type: 'warning',
            message: '请选择计算方式'
        })
    }
}
const rangeTransfor = (item) => {
    let range = `${item.containsMin == 0 ? '(' : '['}${item.min}, ${item.max}${item.containsMax == 0 ? ')' : ']'}`
    return range
}
const confirmLoading = ref(false)
const resultValueConfirm = async () => {
    console.log(configForm.value, '3169_1', 'configForm.value.formulaMainList')
    confirmLoading.value = true
    console.log(configForm.value, 'configForm.value.formulaMainList')
    let params = []
    configForm.value.relatedIndicatorList[relatedIndicatorActiveName]
    configForm.value.relatedIndicatorList.forEach((item, index) => {
    })
    console.log(params, 'params')
    let indicatorListArr = [...configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].formulaMainList[0].inputValuesList]
    indicatorListArr.forEach(item => {
        item.inputValuesName = item.inputValues
        item.indicatorType = item.dailyIndicator
        item.inputValuesParse = {}
        item.inputValuesKey.forEach(k => {
            item.inputValuesParse[k] = item[k]
        })
        item.inputValuesJson = JSON.stringify(item.inputValuesParse)
        item.inputValues = JSON.stringify(item.inputValues)
    })
    console.log(configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].sampleTime, '11111')
    let rq = {
        entryDate: moment(props.taskSearchForm.taskDate).format('YYYY-MM-DD'),
        taskId: configForm.value.taskId,
        instrument: configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].instrument,
        methodName: configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].methodName,
        temperature: configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].temperature,
        humidity: configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].humidity,
        sampleTime: configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].sampleTime ? moment(props.taskSearchForm.taskDate).format('YYYY-MM-DD') + ' ' + moment(configForm.value.relatedIndicatorList[relatedIndicatorActiveName.value].sampleTime).format('HH:mm:ss') : '',
        indicatorList: indicatorListArr
    }
    if (handleClickType.value == 'report' || handleClickType.value == 'update') {
        const res = await taskHttp.supplementaryReport(rq)
        console.log(res, 'res')
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '填报成功'
            })
            emits('closed')
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    } else if (handleClickType.value == 'edit') {
        const res = await taskHttp.correctionReport(rq)
        console.log(res, 'res')
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '填报成功'
            })
            emits('closed')
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    }
    confirmLoading.value = false
}
const showMPNImgVisible = ref(false)
const showMPNImg = () => {
    showMPNImgVisible.value = true
}
const init = (type, row) => {
    console.log(type, row, 1111)
    handleClick(type, row)
}
onMounted(() => {
})
defineExpose({
    init,
    resultValueConfirm
})
</script>

<style lang="less" scoped>
.dialogContent {
    min-width: 1060px;
    width: 100%;
    .dialogModel {
        .modelTitle {
            font-size: 16px;
            line-height: 18px;
            height: 18px;
            padding-left: 10px;
            border-left: 4px solid #58be3f;
            margin-bottom: 10px;
            font-weight: 500;
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            .operateIcon {
                margin-left: 10px;
                img {
                    width: 18px;
                    height: 18px;
                    margin: 0 2px;
                    cursor: pointer;
                }
            }
        }
    }
    .dialogContentTitle {
        line-height: 2.5;
        font-weight: 600;
        margin-top: 16px;
    }
    .dialogContentText {
        line-height: 2;
    }
}
.inputValuesBox {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 10px;
}
.inputValuesTitle {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
}
.inputValuesItem {
    // width: 78px;
    line-height: 3;
    padding: 0 5px;
}
.processValuePosStyle {
    display: flex;
    flex-direction: row;
}
.processValuePosStyle {
    display: flex;
    flex-direction: row;
}
.inputValuesBoxItem0 {
    width: 840px;
}
.inputValuesBoxItem1 {
    width: 1000px;
}
.inputValuesBoxItem2 {
    width: 1000px;
}
</style>